package mww.servlet.student;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mww.common.DaoException;
import mww.common.ServiceException;
import mww.javabean.entity.Student;
import mww.javabean.service.StudentService;
import mww.javabean.service.StudentServiceImpl;
public class AddStudentServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		StudentService studentService = new StudentServiceImpl();
		HttpSession session=request.getSession();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String studentNo=request.getParameter("studentNo");
		String studentName=request.getParameter("studentName");
		String password=request.getParameter("password");
		String studentClass=request.getParameter("studentClass");
		String studentMajor=request.getParameter("studentMajor");
		try{
			if(studentService.findStudentByStudentNo(studentNo)!=null){
				session.setAttribute("message", "添加的学生记录已存在！");
				request.getRequestDispatcher("/admin/addStudent.jsp").forward(request, response);
			}else{
				Student student=new Student();
				student.setStudentNo(studentNo);
				student.setStudentName(studentName);
				student.setPassword(password);
				student.setStudentClass(studentClass);
				student.setStudentMajor(studentMajor);
				studentService.addStudent(student);
		  		session.setAttribute("message", "学生信息添加成功！");
		  		request.getRequestDispatcher("/admin/listAllStudentServlet").forward(request, response);
			}
		}catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("se", e);
			request.getRequestDispatcher("/serviceException.jsp").forward(request, response);
		} catch (DaoException e) {
			e.printStackTrace();
			request.setAttribute("de", e);
			request.getRequestDispatcher("/daoException.jsp").forward(request, response);
		}
	}
}
